/**
 * @Author YuanYuan
 * @Date 2022/4/27
 * @Time 19:48
 */
public class Method {
    public static void main(String[] args) {
        int n = 5;
        int ret = factor(n);
        System.out.println("ret = " + ret);
    }
    public static int factor(int n) {
        System.out.println("函数开始, n = " + n);
        if (n == 1) {
            System.out.println("函数结束, n = 1 ret = 1");
            return 1;
        }
        int ret = n * factor(n - 1);
        System.out.println("函数结束, n = " + n + " ret = " + ret);
        return ret;
    }
    public static void main6(String[] args) {
        int n = 5;
        int ret = factor(n);
        System.out.println("ret = " + ret);
    }
    public static int factor1(int n) {
        if (n == 1) {
            return 1;
        }
        // factor 调用函数自身
        return n * factor(n - 1);

    }
    public static void main5(String[] args) {
        // 调用add(int, int)
        System.out.println(add(1, 2));
    }
    public static void main4(String[] args) {
        // 调用add(int, int)
        System.out.println(add(1, 2));
        // 调用add(double, double)
        System.out.println(add(1.5, 2.5));
        // 调用add(double, double, double)
        System.out.println(add(1.5, 2.5, 3.5));
    }
    public static int add(int x, int y) {
        return x + y;
    }
    public static double add(double x, double y) {
        return x + y;
    }
    public static double add(double x, double y, double z) {
        return x + y + z;
    }
    public static void main3(String[] args) {
        int a = 10;
        int b = 20;
        swap(a, b);
        System.out.println("main: a = " + a + " b = " + b);
    }
    public static void swap(int x, int y) {
        int tmp = x;
        x = y;
        y = tmp;
        System.out.println("swap: x = " + x + " y = " + y);
    }
    public static void main2(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 5; i++) {
            sum += fac(i);
        }
        System.out.println("sum = " + sum);
    }
    public static int fac(int n) {
        System.out.println("计算 n 的阶乘中n! = " + n);
        int result = 1; for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println("第一次调用方法之前");
        int ret = add(a, b);
        System.out.println("第一次调用方法之后");
        System.out.println("ret = " + ret);
        System.out.println("第二次调用方法之前");
        ret = add(30, 50);
        System.out.println("第二次调用方法之后");
        System.out.println("ret = " + ret);
    }
    public static int add1(int x, int y) {
        System.out.println("调用方法中 x = " + x + " y = " + y);
        return x + y;
    }
}
